package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.TbItemCat;
import com.dongdongshop.operator.pojo.TbTypeTemplate;
import com.dongdongshop.operator.service.ItemCatService;
import com.dongdongshop.operator.service.TypeTemplateService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemCat")
public class ItemCatController {

    private final Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    @Reference(check = false)
    private ItemCatService itemCatService;
    @Reference(check = false)
    private TypeTemplateService typeTemplateService;

    //跳转到分类管理并获取分类管理表
    @RequestMapping("toItemCat")
    public String toItemCat(Model model){
        //获取分类管理表
        List<TbItemCat> itemCatList = itemCatService.getItemCat(null);
        model.addAttribute("itemCatList",itemCatList);
        //获取类型模板表
        List<TbTypeTemplate> typeTemplateList = typeTemplateService.getTypeTemplate(null);
        model.addAttribute("typeTemplateList",typeTemplateList);
        return "admin/item_cat";
    }
    //获取下级数据
    @ResponseBody
    @RequestMapping("getDownByPid")
    public Result getDownByPid(Long parentId){
        try {
            List<TbItemCat> itemCatDownList = itemCatService.getItemCat(parentId);
            return Result.success(ResultEnum.SUCCESS,itemCatDownList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //获取回显数据
    @ResponseBody
    @RequestMapping("getItemCatById")
    public Result getItemCatById(Long id){
        try {
            TbItemCat itemCat = itemCatService.getItemCatById(id);
            return Result.success(ResultEnum.SUCCESS,itemCat);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //获取头部信息
    @ResponseBody
    @RequestMapping("head")
    public Result head (Long id){
        try {
            List<TbItemCat> list = itemCatService.getTot(id);
            return Result.success(ResultEnum.SUCCESS,list);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //增加操作
    @ResponseBody
    @RequestMapping("addItemCat")
    public Result addItemCat(TbItemCat itemCat){
        try {
            itemCatService.addItemCat(itemCat);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //删除操作
    @RequestMapping("deleteItemCatByIds")
    @ResponseBody
    public Result deleteItemCatByIds(@RequestParam(value = "ids[]")Long[] ids){
        try {
            itemCatService.deleteItemCatByIds(ids);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //修改操作
    @ResponseBody
    @RequestMapping("updateItemCatById")
    public Result updateItemCatById(TbItemCat itemCat){
        try {
            itemCatService.updateItemCatById(itemCat);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
